package com.fjec.activity.rabbitmq.provider;

import com.fjec.common.protocol.FjecFruitsProto;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SecKillProvider {

    @Value("${fjec.activity.seckill.exchange.name}")
    private String exchangeName;

    @Value("${fjec.activity.seckill.router.key}")
    private String routingKey;

    @Resource(name = "secKillAmqpTemplate")
    private RabbitTemplate secKillAmqpTemplate;

    public void sendSecKill(Long userId, Long secKillId, String shopName) {
        FjecFruitsProto.SecKill secKill = FjecFruitsProto.SecKill.newBuilder()
                .setUserId(userId)
                .setShopName(shopName)
                .setSecKillId(secKillId)
                .build();
        FjecFruitsProto.FjecFruitsMsg msg = FjecFruitsProto.FjecFruitsMsg.newBuilder()
                .setSecKill(secKill)
                .build();
        CorrelationData correlationData = new CorrelationData(userId + "||" + secKillId);
        secKillAmqpTemplate.convertAndSend(exchangeName, routingKey, msg.toByteArray(), correlationData);
    }
}
